# -*- coding;utf-8 -*-
"""
File name : signals.py.PY
Program IDE : PyCharm
Create file time: 2023/10/16 10:34
File Create By Author : 朱清闯
"""
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from .models import User, CustomerBankPointChangeLog, CompanySupplyBankPointLog, Company, RechargeLog, WithdrawLog
from django.db.models import Q, F
from question.models import ChallengePaperOfUser


# 该函数用于被以下函数调用
def company_account_point_change(point_amount=1):
    username = Company.objects.first().president
    user_company = User.objects.get(username=username)
    user_company.bank_point += point_amount
    user_company.save()


# 当CustomerBankPointChangeLog模型发生创建操作时，触发相应用户的库币变化（包括接收者和发送者的库币变化）
@receiver(post_save, sender=CustomerBankPointChangeLog)
def create_bank_point_change(sender, instance, created, **kwargs):
    if created:
        print("信号bankpointLog结算执行create，")
        sender_user = instance.operator
        band_point_result = sender_user.bank_point - instance.point_amount
        sender_user.bank_point = band_point_result
        sender_user.save()
        receiver_user = instance.receiver
        receiver_user.bank_point = F("bank_point") + instance.point_amount
        receiver_user.save()
        return True


# 当CustomerBankPointChangeLog模型发生保存操作时，触发相应用户的库币变化（包括接收者和发送者的库币变化）
# @receiver(post_save, sender=CustomerBankPointChangeLog)
# def save_bank_point_change(sender, instance, **kwargs):
#     print("信号bankpoint的save被执行")
#     sender_user = instance.operator
#     band_point_result = sender_user.bank_point - instance.point_amount
#     if band_point_result >= 0:
#         sender_user.bank_point = band_point_result
#         sender_user.save()
#     else:
#         return False
#     receiver_user = instance.receiver
#     receiver_user.bank_point = F("bank_point") + instance.point_amount
#     receiver_user.save()
#     return True


# 当CustomerBankPointChangeLog模型发生删除操作时，触发相应用户的库币变化（包括接收者和发送者的库币变化）
@receiver(post_delete, sender=CustomerBankPointChangeLog)
def delete_bank_point_change(sender, instance, **kwargs):
    print("信号bankpoint的delete被执行")
    receiver_user = instance.receiver
    band_point_result = receiver_user.bank_point - instance.point_amount
    receiver_user.bank_point = band_point_result
    receiver_user.save()

    sender_user = instance.operator
    sender_user.bank_point = F("bank_point") + instance.point_amount
    sender_user.save()
    return True


# 当CompanySupplyBankPointLog模型发生创建操作时，触发相应用户的库币变化
@receiver(post_save, sender=CompanySupplyBankPointLog)
def create_bank_point_supply(sender, instance, created, **kwargs):
    print("信号supply被执行")
    if created and instance.point_amount > 0:
        company_account_point_change(point_amount=instance.point_amount)


# 当ChallengePaperOfUser被创建后，增加该用户的积分，计入到个人的总分中
@receiver(post_save, sender=ChallengePaperOfUser)
def create_challenge_paper(sender, instance, created, **kwargs):
    print("信号ChallengePaperOfUser被执行")
    if created and instance.score > 0:
        user = instance.user
        user.challenge_score = F('challenge_score') + instance.score
        user.save()

# 当CompanySupplyBankPointLog模型发生保存操作时，触发相应用户的库币变化
# @receiver(post_save, sender=CompanySupplyBankPointLog)
# def save_bank_point_supply(sender, instance, **kwargs):
#     if instance.point_amount > 0:
#         company_account_point_change(point_amount=instance.point_amount)

#
# # 当RechargeLog模型发生保存操作且审批通过时，触发相应用户的库币变化
# @receiver(post_save, sender=RechargeLog)
# def save_recharge_log(sender, instance, **kwargs):
#     amount = instance.amount
#     user = instance.user
#     bank_point = user.bank_point
#     # 充值成功，增加相应的库币
#     if amount > 0 and instance.status == 1:
#         user.bank_point = bank_point + amount
#         user.save()
#     # 撤销充值，扣除相应的
#     elif amount > 0 and bank_point-amount > 0 and instance.status == 2:
#         user.bank_point -= amount
#         user.save()
#
#
# # 当WithdrawLog模型发生保存操作且审批通过时，触发相应用户的库币变化
# @receiver(post_save, sender=WithdrawLog)
# def save_withdraw_log(sender, instance, **kwargs):
#     amount = instance.amount
#     user = instance.user
#     bank_point = user.bank_point
#     # 提现成功，扣除相应的库币金额
#     if amount > 0 and bank_point - amount >= 0 and instance.status == 1:
#         user.bank_point = user.bank_point - amount
#         user.save()
#     # 撤销提现，返回相应的库币金额
#     elif instance.status == 2 and amount > 0:
#         user.bank_point += amount
#         user.save()
